//CHART_REPO := http://jenkins-chartmuseum:8080
CURRENT=$(pwd)
NAME := $(FILE_NAME)
OS := $(shell uname)
RELEASE_VERSION := $(shell cat ../../../VERSION)
DEPLOY_ALIYUN := $(ALIYUN)
RELEASE_VERSION_3 := $(RELEASE_VERSION_3)

build: clean
	rm -rf requirements.lock
	helm dependency build
	helm lint

install: clean build
	helm install . --name ${NAME}

upgrade: clean build
	helm upgrade ${NAME} .

delete:
	helm delete --purge ${NAME}

clean:
	rm -rf charts
	rm -rf ${NAME}*.tgz

release: clean
	helm dependency build
	helm lint
	helm init --client-only --skip-refresh --stable-repo-url=${CHARTMUSEUM_REPO}
	helm package .
ifeq ($(CHARTMUSEUM_TYPE),nexus3)
	curl --fail -u $(CHARTMUSEUM_CREDS_USR):$(CHARTMUSEUM_CREDS_PSW) -F file=@$(NAME)-$(shell sed -n 's/^version: //p' Chart.yaml).tgz $(NEXUS3_API)
else ifeq ($(DEPLOY_ALIYUN),true)
	curl --fail -u $(CHARTMUSEUM_CREDS_USR):$(CHARTMUSEUM_CREDS_PSW) --data-binary "@$(NAME)-$(shell sed -n 's/^version: //p' Chart.yaml).tgz" $(ALIYUN_CHARTMUSEUM_REPO)/api/charts
else
	curl --fail -u $(CHARTMUSEUM_CREDS_USR):$(CHARTMUSEUM_CREDS_PSW) --data-binary "@$(NAME)-$(shell sed -n 's/^version: //p' Chart.yaml).tgz" $(CHARTMUSEUM_REPO)/api/charts
endif
#ifeq ($(DEPLOY_ALIYUN),true)
#endif
	rm -rf ${NAME}*.tgz%

tag:
ifeq ($(OS),Darwin)
	sed -i "" -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml
	sed -i -e "s/appVersion:.*/appVersion: $(RELEASE_VERSION)/" Chart.yaml
	sed -i "" -e "s/tag: .*/tag: $(RELEASE_VERSION)/" values.yaml
	sed -i -e "s/existingClaim:.*/existingClaim: $(NAMESPACE)-pvc/" values.yaml	
else ifeq ($(OS),Linux)
	sed -i -e "s/version:.*/version: $(RELEASE_VERSION)/" Chart.yaml
	sed -i -e "s/appVersion:.*/appVersion: $(RELEASE_VERSION)/" Chart.yaml
	sed -i -e "s|repository:.*|repository: $(DOCKER_REGISTRY)\/$(ORG)\/$(NAME)|" values.yaml
	sed -i -e "s/tag: .*/tag: $(RELEASE_VERSION)/" values.yaml
	sed -i -e "s/existingClaim:.*/existingClaim: $(NAMESPACE)-pvc/" values.yaml	
else
	echo "platfrom $(OS) not supported to release from"
	exit -1
endif

tag3:
ifeq ($(OS),Darwin)
	sed -i "" -e "s/version:.*/version: $(RELEASE_VERSION_3)/" Chart.yaml
	sed -i -e "s/appVersion:.*/appVersion: $(RELEASE_VERSION_3)/" Chart.yaml
	sed -i "" -e "s/tag: .*/tag: $(RELEASE_VERSION_3)/" values.yaml
	sed -i -e "s/existingClaim:.*/existingClaim: $(NAMESPACE)-pvc/" values.yaml
else ifeq ($(OS),Linux)
	sed -i -e "s/version:.*/version: $(RELEASE_VERSION_3)/" Chart.yaml
	sed -i -e "s/appVersion:.*/appVersion: $(RELEASE_VERSION_3)/" Chart.yaml
	sed -i -e "s|repository:.*|repository: $(DOCKER_REGISTRY)\/$(ORG)\/$(NAME)|" values.yaml
	sed -i -e "s/tag: .*/tag: $(RELEASE_VERSION_3)/" values.yaml
	sed -i -e "s/existingClaim:.*/existingClaim: $(NAMESPACE)-pvc/" values.yaml
else
	echo "platfrom $(OS) not supported to release from"
	exit -1
endif
